package control;

import gui.config.DetailEditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import util.Adresse;
import util.FormParser;
import util.Geburtstag;
import util.Termin;
import xml.XMLWorker;

public class UpdateEvent implements ActionListener {
	
	XMLWorker worker = null;
	DetailEditor input = null;
	private SimpleDateFormat time = null;
	private SimpleDateFormat date = null;

	public UpdateEvent(XMLWorker worker, DetailEditor detailEditor) {
		this.worker = worker;
		this.input = detailEditor;
		time = new SimpleDateFormat("HH:mm");
		date = new SimpleDateFormat("dd.MM.yyyy");
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		input.setConfirmlayerVisbile(true);
		if(inputIsValid()) {
			updateEvent();
		}
	}

	private void updateEvent() {
		if(input.isBday()) {
			// speichere einen Geburtstag
			Geburtstag g = (Geburtstag) input.getActualEvent();
			try {
				g.setDatum(date.parse(input.getDateOfBirthday()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			g.setName(input.getNameOfBirthday());
			g.setKommentar(input.getCommentOfBirthday());
			if(input.hasAdress()) {
				Adresse a = new Adresse();
				a.setStrasse(input.getStreetOfBirthday());
				a.setOrt(input.getPlaceOfBirthday());
				a.setLand(input.getCountryOfBirthday());
				a.setNummer(Integer.valueOf(input.getNumberOfBirthday()));
				g.setAdresse(a);
			} else {
				g.setAdresse(null);
			}
			worker.replace(g);
			input.removeWindow();
		} else {
			// speichere einen Termin
			Termin t = (Termin)input.getActualEvent();
			try {
				t.setDatum(date.parse(input.getDateOfEvent()));
				t.setUhrzeit(time.parse(input.getTimeOfEvent()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			t.setTitel(input.getTitleOfEvent());
			t.setOrt(input.getPlaceOfEvent());
			t.setKommentar(input.getCommentOfEvent());
			worker.replace(t);
			input.removeWindow();
		}
	}

	protected boolean inputIsValid() {
		if(input.isBday()) {
			// Geburtstag
			if(FormParser.isDate(input.getDateOfBirthday()) &&
						FormParser.isName(input.getNameOfBirthday()) &&
						FormParser.isComment(input.getCommentOfBirthday())) {
				if(input.hasAdress()) {
					if(FormParser.isStreet(input.getStreetOfBirthday()) &&
							FormParser.isPlace(input.getPlaceOfBirthday()) &&
							FormParser.isCountry(input.getCountryOfBirthday()) &&
							FormParser.isNumber(input.getNumberOfBirthday())) {
						return true;
					} else {
						return false;
					}
				}
				return true;
			}
			return false;
		} else if(input.isEvent()) {
			// Termin
			if(FormParser.isDate(input.getDateOfEvent()) &&
					FormParser.isTime(input.getTimeOfEvent()) &&
					FormParser.isTitle(input.getTitleOfEvent()) &&
					FormParser.isPlace(input.getPlaceOfEvent()) &&
					FormParser.isComment(input.getCommentOfEvent())) {
				return true;
			}
			return false;
		}
		return false;
	}

}
